<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * general.php
 *
 * The general controller
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class general extends CI_Controller {




    /**
     * Remap
     *
     * Checks that we've got a valid AJAX page. As
     * this activates a function in the AJAX library,
     * this can just be copied to all AJAX controllers.
     *
     * @param string $method
     */
    public function _remap($method) {
        $this->load->library('ajax');
        $this->ajax->ajax_exists($method);
        $this->$method();
    }
    
    
    
    
    
    
    
    /**
     * Index
     * 
     * Default error page for the AJAX 
     */
    public function index() { echo json_encode(array('error' => true)); }
    
    
    
    
    
    
    
    /**
     * Get Table
     * 
     * Gets the tables for the specified database
     */
    public function get_table() {
        /* What are the required fields */
        $arrRequired = array(
            'url',
        );

        /* Check the required input fields */
        $arrDetails = $this->ajax->check($arrRequired);
        
        /* Get the REST variables */
        $db = $this->input->rest($this->config->item('database', 'rest'), false, $arrDetails['url']);
        $tbl = $this->input->rest($this->config->item('table', 'rest'), false, $arrDetails['url']);
        
        $arrReturn = array();
        $error_msg = false;
        
        if($db !== false && $tbl !== false) {
            /* REST exists */
            $this->load->model('database');
            
            /* Get the table */
            $arrTable = $this->database->fetch_table($db, $tbl);
            
            if(count($arrTable) > 0) {
                $arrReturn = array(
                    'table' => output_data_table($arrTable),
                );
            } else {
                /* No table */
                $error_msg = $this->lang->line('ajax_no_table');
            }
        } else {
            /* Not everything sent */
            $error_msg = $this->lang->line('ajax_general_error');
        }
        
        /* Add the error status */
        $arrReturn['error'] = (bool) $error_msg;
        $arrReturn['error_msg'] = $error_msg;
        
        /* Output the array */
        echo json_encode($arrReturn);
        
    }
    
    
    
    
    
    
    
    
    /**
     * Decode Connection
     * 
     * Decodes the connection details 
     */
    public function decode_connection() {
        
        /* What are the required fields */
        $arrRequired = array(
            'connection',
        );

        /* Check the required input fields */
        $arrDetails = $this->ajax->check($arrRequired);
        
        /* Load stuff */
        $this->load->model('connections');

        $arrConnection = $this->connections->decode($arrDetails['connection']);

        if(is_array($arrConnection)) {
            /* Valid key - replace the values */
            $arrReturn['connection'] = $arrConnection;
            
            $error = false;
        } else {
            $error = true;
        }
        
        $arrReturn['error'] = $error;
        
        echo json_encode($arrReturn);
    }
    
    
    
    
    
    
    
    
    /**
     * Sort Connection
     * 
     * Manually reorder the connection 
     */
    public function sort_connection() {
        
        /* What are the required fields */
        $arrRequired = array(
            'connections',
        );

        /* Check the required input fields */
        $arrDetails = $this->ajax->check($arrRequired);
        
        if(is_array($arrDetails['connections']) && count($arrDetails['connections']) > 0) {
            $this->database->reorder_connection($arrDetails['connections']);
        }
    }
    
    
    
    
    
    
    
    
    
    /**
     * Execute Query
     * 
     * Executes the SQL uery 
     */
    public function execute_query() {
        
        /* What are the required fields */
        $arrRequired = array(
            'connection',
            'database',
            'query',
        );

        /* Check the required input fields */
        $arrDetails = $this->ajax->check($arrRequired, 'post', false);
        
        /* Load stuff */
        $this->load->model('database');
        
        /* Execute the query */
        $arrDb = $this->database->execute($arrDetails['database'], $arrDetails['query'], $arrDetails['connection']);
        
        /* Parse the data as an HTML table */
        $arrDb['table'] = base64_encode(output_query_results($arrDb, 70));
        echo json_encode($arrDb);exit;
        
    }

}
?>